package com.sgb.kjwl.view.ui.fragment.machine;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.alimaplibrary.SelectAddressActivity;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLeaseBusinessBinding;
import com.sgb.kjwl.di.machine.DaggerMachineFragComponent;
import com.sgb.kjwl.di.machine.MachineFragModule;
import com.sgb.kjwl.model.entity.lease.LeaseListEntity;
import com.sgb.kjwl.model.entity.request.CarFilterJson;
import com.sgb.kjwl.model.entity.request.FactoryFilterJson;
import com.sgb.kjwl.model.entity.request.FarmFilterJson;
import com.sgb.kjwl.model.entity.request.HouseFilterJson;
import com.sgb.kjwl.model.entity.request.LandFilterJson;
import com.sgb.kjwl.model.entity.request.LeaseMachineFilterJson;
import com.sgb.kjwl.model.entity.request.MachineFilterJson3;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.adapter.machine.LeaseListAdapter;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.lease.LeaseCompanyFliterPopWindow;
import com.sgb.kjwl.view.widget.lease.LeaseFilterPopWindow;
import com.sgb.kjwl.viewmodel.machine.LeaseBusinessFragmentModel;
import com.sgb.link.event.LeaseEvent;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration2;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 租赁-企业专项
 */
public class LeaseBusinessFragment extends AppLazyFragment {

    private FragmentLeaseBusinessBinding binding;
    private String[] businessTypeList = {"全部", "会员单位", "成员单位"};
    private String[] categoryTypeList = {"全部", "机械租赁", "设备租赁", "车辆租赁", "房产租赁", "土地租赁", "工厂租赁", "农机租赁"};

    private String firstCategoryNo = "";
    private String secondCategoryNo = "";
    private String sortNo = "";
    private String provinceNo = "";
    private String cityNo = "";
    private String areaId = "";
    private List<String> cityList = new ArrayList<>();
    private String provinceName = "";
    private String cityName = "";
    private String memberCompanyNo = "";
    private String memberType = "";//1成员单位 ，2会员单位


    private DictWindow5 sortPopWindow;
    private ThreeLevelAreaWindow mAddressPop;

    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;//省市区
    //  private List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> allData = new ArrayList<>();
    private List<DictEntity3> classDatas = new ArrayList<>();
    private List<DictEntity3> typeDatas = new ArrayList<>();
    private List<DictEntity3> companyList = new ArrayList<>();
    private List<DictEntity3> sortData;//综合排序
    private LeaseListAdapter adapter;
    private String isSelf = "";//是否自营 ，1 为自营
    private String categoryType = "";//租赁类型
    private LeaseFilterPopWindow popWindow;

    @Inject
    LeaseBusinessFragmentModel viewModel;

    public static LeaseBusinessFragment newInstance() {
        LeaseBusinessFragment fragment = new LeaseBusinessFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString("type", type);
//        bundle.putSerializable("data", list);
//        bundle.putBoolean("isFilter", isFilter);
//        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMachineFragComponent.builder()
                .appComponent(appComponent)
                .machineFragModule(new MachineFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_business, container, false);
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        provinceNo = viewModel.getAreaCodeModel();//获取大首页默认的省份
//        viewModel.getTypeModel();
        viewModel.getFilterSort();
        setTabBusiness();
        setTabCategory();
    }

    @Override
    protected void initView() {
        initTabLayout();
        binding.areaLayout.setOnClickListener(this);
        binding.sortLayout.setOnClickListener(this);
        binding.cvRightArrow.setOnClickListener(this);
        binding.ivMap.setOnClickListener(this);
        binding.ivFilter.setOnClickListener(this);
        binding.isSelfLayout.setOnClickListener(this);

        adapter = new LeaseListAdapter(getActivity(), null);
        adapter.setOnItemClick(new AdapterOnItemClick<LeaseListEntity>() {
            @Override
            public void onItemClick(LeaseListEntity entity, int position) {
                if (!ClickUtils.isDoubleClick()) {
                    viewModel.getH5Url(entity.getType(), entity.getDataId());
                }
            }
        });
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.addItemDecoration(new RecyclerViewItemDecoration2(getResources().getDimension(R.dimen.dp10), getResources().getDimension(R.dimen.dp10)));
        binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        binding.recyclerView.setFootViewText("加载中", "我也是有底线的");
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });


        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(binding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setProvinceSingle();
        mAddressPop.setCitySingle();
        mAddressPop.setAreaSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnResultClick((key1, key2, key3) -> {
            try {
                List<BaseAreaEntity> listKey1 = (List<BaseAreaEntity>) key1;
                List<BaseAreaEntity> listKey2 = (List<BaseAreaEntity>) key2;
                List<BaseAreaEntity> listKey3 = (List<BaseAreaEntity>) key3;
                cityList = getCityList(listKey3);
                if (listKey2 != null && listKey2.size() > 0) {
                    cityName = listKey2.get(0).getName();
                    cityNo = listKey2.get(0).getNo();
                } else {
                    cityNo = "";
                }
                provinceName = listKey1.get(0).getName();
                provinceNo = listKey1.get(0).getNo();
                if (!TextUtils.isEmpty(cityName) && "不限".equals(cityName)) {
                    updateChooseTextStatus(binding.areaLayout, binding.tvArea, 2, cityName);
                } else {
                    updateChooseTextStatus(binding.areaLayout, binding.tvArea, 2, provinceName);
                }
            } catch (Exception exception) {
            }

            loadData(false);

        });
        mAddressPop.setOnDismissListener(() -> {
            updateChooseTextStatus(binding.areaLayout, binding.tvArea, "地区".equals(binding.tvArea.getText()) ? 0 : 2, null);
        });

        sortPopWindow = new DictWindow5(getActivity());
        sortPopWindow.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, 2, dictEntity.getName());
                sortNo = dictEntity.getId();
                loadData(false);
            }
        });
        sortPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, "排序".equals(binding.tvSort.getText()) ? 0 : 2, null);
            }
        });
        initFliter();
    }

    private void initFliter() {
        popWindow = new LeaseFilterPopWindow(getActivity());
        popWindow.setFilterResultClick(new LeaseFilterPopWindow.FilterResultClick() {
            @Override
            public void result(MachineFilterJson3 filterJson, List<DictEntity3> data, SparseBooleanArray checkBooleanArray) {
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, 2, filterJson.getSortName());
                firstCategoryNo = filterJson.getTypeId();
                secondCategoryNo = filterJson.getModelId();
                sortNo = filterJson.getSortWay();

//                if (filterJson.getAttrList() == null || filterJson.getAttrList().size() == 0) {
//                    viewModel.filterJson.get().setAttributeList(null);
//                } else {
//                    viewModel.filterJson.get().setAttributeList(filterJson.getAttrList());
//                }
                loadData(false);

                //关联同步当前界面的选项
                binding.tabFirst.getTabAt(getCheckIndex(classDatas, filterJson.getTypeId())).select();
                binding.tabSecond.getTabAt(getCheckIndex(typeDatas, filterJson.getModelId())).select();
                sortPopWindow.setCheckDefaultById(filterJson.getSortId());
            }

            @Override
            public void queryTypeChild(String id) {
                viewModel.getSecondCategoryModel(categoryType, id);
            }

            @Override
            public void queryNameChild(String id) {
                viewModel.getFilterSpec(categoryType, id);
            }
        });
    }

    private void initTabLayout() {
        binding.tabBusiness.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                memberCompanyNo = "";//清除子级参数设置
                if (tab.getPosition() == 0) {
                    binding.rlTabCompany.setVisibility(View.GONE);
                    binding.tabCompany.setVisibility(View.GONE);
                    binding.tabCompanyLine.setVisibility(View.GONE);
                    memberType = "";
                    loadData(false);
                } else {
                    binding.rlTabCompany.setVisibility(View.VISIBLE);
                    binding.tabCompany.setVisibility(View.VISIBLE);
                    binding.tabCompanyLine.setVisibility(View.VISIBLE);
                    memberType = String.valueOf(tab.getPosition());
                    //因后端接口顺序和产品设计顺序不一致，需要转换顺序
                    //1成员单位 ，2会员单位
                    if ("会员单位".equals(tab.getText())) {
                        memberType = "2";
                        viewModel.getTypeCompany(2);
                    } else if ("成员单位".equals(tab.getText())) {
                        memberType = "1";
                        viewModel.getTypeCompany(1);
                    }

                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        binding.tabCompany.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                memberCompanyNo = companyList.get(tab.getPosition()).getId();
                loadData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        binding.tabCategory.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //清除子级参数设置
                firstCategoryNo = "";
                secondCategoryNo = "";
                //清除数据
                typeDatas.clear();
                popWindow.setModelData(typeDatas);
                if (tab.getPosition() == 0) {
                    binding.tabFirst.setVisibility(View.GONE);
                    binding.tabFirstLine.setVisibility(View.GONE);
                    binding.tabSecond.setVisibility(View.GONE);
                    binding.tabSecondLine.setVisibility(View.GONE);
                    categoryType = "";
                    loadData(false);
                } else {
                    binding.tabFirst.setVisibility(View.VISIBLE);
                    binding.tabFirstLine.setVisibility(View.VISIBLE);
                    binding.tabSecond.setVisibility(View.GONE);
                    binding.tabSecondLine.setVisibility(View.GONE);
                    viewModel.getFistCategoryModel(String.valueOf(tab.getPosition()));
                    categoryType = String.valueOf(tab.getPosition());
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        binding.tabFirst.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                firstCategoryNo = classDatas.get(tab.getPosition()).getId();
                secondCategoryNo = "";//清除子级参数设置
                // 全部，//车辆，工厂，隐藏二级分类展示
                if (tab.getPosition() == 0 || "3".equals(categoryType) || "6".equals(categoryType)) {
                    binding.tabSecond.setVisibility(View.GONE);
                    binding.tabSecondLine.setVisibility(View.GONE);
                    loadData(false);
                } else {
                    binding.tabSecond.setVisibility(View.VISIBLE);
                    binding.tabSecondLine.setVisibility(View.VISIBLE);
                    viewModel.getSecondCategoryModel(categoryType, firstCategoryNo);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        binding.tabSecond.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                checkDismiss(0);
                secondCategoryNo = typeDatas.get(tab.getPosition()).getId();
                loadData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

//    //类型集合
//    public void showTypeView(List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> data) {
//        this.allData = data;
//    }


    private void loadData(boolean isLoadMore) {
        if ("".equals(categoryType) || "1".equals(categoryType) || "2".equals(categoryType)) {
            LeaseMachineFilterJson filterJson = new LeaseMachineFilterJson();
            filterJson.setType(categoryType);
            filterJson.setFirstCategoryNo(firstCategoryNo);
            filterJson.setSecondCategoryNo(secondCategoryNo);
            filterJson.setIsSelf(isSelf);
            filterJson.setProvinceNo(provinceNo);
            filterJson.setCityNo(cityNo);
            filterJson.setAreaNoList(cityList);
            filterJson.setSortWay(sortNo);
            filterJson.setIsSpecial("1");//企业专项
            filterJson.setMemberCompanyNo(memberCompanyNo);
            filterJson.setMemberType(memberType);
            viewModel.getData(filterJson, isLoadMore);
        } else if ("3".equals(categoryType)) {
            CarFilterJson filterJson = new CarFilterJson();
            filterJson.setType(categoryType);
            filterJson.setModelId(firstCategoryNo);
            filterJson.setBrandId(secondCategoryNo);
            filterJson.setIsSelf(isSelf);
            filterJson.setProvinceNo(provinceNo);
            filterJson.setCityNo(cityNo);
            filterJson.setDistrictNo(cityList);
            filterJson.setSortWay(sortNo);
            filterJson.setIsSpecial("1");//企业专项
            filterJson.setMemberCompanyNo(memberCompanyNo);
            filterJson.setMemberType(memberType);
            viewModel.getData(filterJson, isLoadMore);
        } else if ("4".equals(categoryType)) {
            HouseFilterJson filterJson = new HouseFilterJson();
            filterJson.setType(categoryType);
            filterJson.setCategoryParentId(firstCategoryNo);
            filterJson.setCategoryChildId(secondCategoryNo);
            filterJson.setIsSelf(isSelf);
            filterJson.setProvinceNo(provinceNo);
            filterJson.setCityNo(cityNo);
            filterJson.setAreaNoList(cityList);
            filterJson.setSortWay(sortNo);
            filterJson.setIsSpecial("1");//企业专项
            filterJson.setMemberCompanyNo(memberCompanyNo);
            filterJson.setMemberType(memberType);
            viewModel.getData(filterJson, isLoadMore);
        } else if ("5".equals(categoryType)) {
            LandFilterJson filterJson = new LandFilterJson();
            filterJson.setType(categoryType);
            filterJson.setFirstCategoryNo(firstCategoryNo);
            filterJson.setSecondCategoryNo(secondCategoryNo);
            filterJson.setIsSelf(isSelf);
            filterJson.setProvinceNo(provinceNo);
            filterJson.setCityNo(cityNo);
            filterJson.setAreaNos(cityList);
            filterJson.setSortWay(sortNo);
            filterJson.setIsSpecial("1");//企业专项
            filterJson.setMemberCompanyNo(memberCompanyNo);
            filterJson.setMemberType(memberType);
            viewModel.getData(filterJson, isLoadMore);
        } else if ("6".equals(categoryType)) {
            FactoryFilterJson filterJson = new FactoryFilterJson();
            filterJson.setType(categoryType);
            filterJson.setFirstCategoryNo(firstCategoryNo);
            filterJson.setSecondCategoryNo(secondCategoryNo);
            filterJson.setIsSelf(isSelf);
            filterJson.setAreacode(provinceNo);
            filterJson.setCity(cityNo);
            filterJson.setArea(cityList);
            filterJson.setSortWay(sortNo);
            filterJson.setIsSpecial("1");//企业专项
            filterJson.setMemberCompanyNo(memberCompanyNo);
            filterJson.setMemberType(memberType);
            viewModel.getData(filterJson, isLoadMore);
        } else if ("7".equals(categoryType)) {
            FarmFilterJson filterJson = new FarmFilterJson();
            filterJson.setType(categoryType);
            filterJson.setFirstCategoryNo(firstCategoryNo);
            filterJson.setSecondCategoryNo(secondCategoryNo);
            filterJson.setIsSelf(isSelf);
            filterJson.setAreacode(provinceNo);
            filterJson.setCity(cityNo);
            filterJson.setArea(cityList);
            filterJson.setSortWay(sortNo);
            filterJson.setIsSpecial("1");//企业专项
            filterJson.setMemberCompanyNo(memberCompanyNo);
            filterJson.setMemberType(memberType);
            viewModel.getData(filterJson, isLoadMore);
        }
    }


    public void showList(List<LeaseListEntity> list, boolean isLoadMore) {
        binding.recyclerView.setNoMore(list.size() < viewModel.pageSize);
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            adapter.setDatas(list);
        }
    }

    /**
     * @param v
     */
    @Override
    public void onClick(View v) {
        checkDismiss(v.getId());
        switch (v.getId()) {
            case R.id.area_layout://地区
                updateChooseTextStatus(binding.areaLayout, binding.tvArea, "地区".equals(binding.tvArea.getText()) ? -1 : 1, null);
                mAddressPop.showPopwindow(binding.areaLayout);
                if (null == addressEntityList || addressEntityList.size() == 0) {
                    viewModel.getCityData();
                }
                break;
            case R.id.sort_layout://排序
                if (null != sortData && sortData.size() > 0) {
                    updateChooseTextStatus(binding.sortLayout, binding.tvSort, "排序".equals(binding.tvSort.getText()) ? -1 : 1, null);
                    sortPopWindow.showPopwindow(binding.tvSort);
                }
                break;
            case R.id.iv_map://地图定位
                startMap();
                break;
            case R.id.iv_filter://筛选
                popWindow.setTypeDefault(binding.tabFirst.getSelectedTabPosition(), firstCategoryNo);
                popWindow.setModelDefault(binding.tabSecond.getSelectedTabPosition(), secondCategoryNo);
                popWindow.setSortDefault(sortPopWindow.getCheckDefultPosition(sortNo), sortNo);
                popWindow.backgroundAlpha(0.5f);
                popWindow.showPopwindowRight(getActivity().getWindow().getDecorView());
                break;
            case R.id.isSelf_layout://是否自营
                if (TextUtils.isEmpty(isSelf)) {
                    isSelf = "1";
                    binding.isSelfLayout.setBackgroundResource(R.drawable.search_new_blue_bg);
                    binding.tvIsSelf.setTextColor(getResources().getColor(R.color.red));
                } else {
                    isSelf = "";
                    binding.isSelfLayout.setBackgroundResource(R.drawable.search_bg);
                    binding.tvIsSelf.setTextColor(getResources().getColor(R.color.color_333333));
                }
                loadData(false);
                break;
            case R.id.cv_right_arrow://成员公司右侧筛选
                LeaseCompanyFliterPopWindow popWindow = new LeaseCompanyFliterPopWindow(getActivity());
                popWindow.setFilterResultClick(new LeaseCompanyFliterPopWindow.FilterResultClick() {
                    @Override
                    public void result(String no) {
                        memberCompanyNo = no;
                        binding.tabCompany.getTabAt(getCheckIndex(companyList, memberCompanyNo)).select();
                        loadData(false);
                    }
                });
                popWindow.setTypeData(companyList);
                popWindow.setTypeDefault(binding.tabCompany.getSelectedTabPosition(), businessTypeList[binding.tabBusiness.getSelectedTabPosition()]);
                popWindow.showPopwindowRight(getActivity().getWindow().getDecorView());
                popWindow.backgroundAlpha(0.5f);
                break;
        }
    }

    //会员，成员 tab设置
    public void setTabBusiness() {
        //type类型 0个人 1企业 2团队 3组织
        int type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if (type == 0) {
            businessTypeList = new String[]{"全部", "会员单位"};
        }
        binding.tabBusiness.removeAllTabs();
        for (int i = 0; i < businessTypeList.length; i++) {
            binding.tabBusiness.addTab(binding.tabBusiness.newTab().setText(businessTypeList[i]));
        }
        binding.tabBusiness.getTabAt(0).select();
    }

    // 公司名称 tab设置
    public void setTabCompany(List<DictEntity3> list) {
        list.add(0, new DictEntity3("", "全部"));
        this.companyList = list;
        binding.tabCompany.removeAllTabs();
        for (int i = 0; i < list.size(); i++) {
            String companyName = list.get(i).getName();
            if (!TextUtils.isEmpty(companyName) && companyName.length() > 6) {
                companyName = companyName.substring(0, 6) + "...";
            }
            binding.tabCompany.addTab(binding.tabCompany.newTab().setText(companyName));
        }
        binding.tabCompany.getTabAt(0).select();
        binding.tabCompany.postDelayed(new Runnable() {
            public void run() {
                binding.tabCompany.getTabAt(0).select();
            }
        }, 100);
    }

    //租赁类目 tab设置
    public void setTabCategory() {
        binding.tabCategory.removeAllTabs();
        for (int i = 0; i < categoryTypeList.length; i++) {
            binding.tabCategory.addTab(binding.tabCategory.newTab().setText(categoryTypeList[i]));
        }
        binding.tabCategory.getTabAt(0).select();
    }

    //租赁一级类目 tab设置
    public void setTabFirst(List<DictEntity3> list) {
        if (list == null) return;
        this.classDatas = list;
        if ("3".equals(categoryType)) {//车辆
            binding.tabSecond.setVisibility(View.GONE);
            binding.tabSecondLine.setVisibility(View.GONE);
        }
        if ("6".equals(categoryType)) {//工厂
            binding.tabSecond.setVisibility(View.GONE);
            binding.tabSecondLine.setVisibility(View.GONE);
        }

        if (list.size() > 0 && "不限".equals(list.get(0).getName())) {
            list.get(0).setName("全部");
        }

        binding.tabFirst.removeAllTabs();
        for (int i = 0; i < classDatas.size(); i++) {
            binding.tabFirst.addTab(binding.tabFirst.newTab().setText(classDatas.get(i).getName()));
        }
        binding.tabFirst.getTabAt(0).select();

        popWindow.setTypeData(classDatas);
    }

    //租赁二级类目 tab设置
    public void setTabSecond(List<DictEntity3> list) {
        if (list == null) return;
        this.typeDatas = list;

        if (list.size() > 0 && "不限".equals(list.get(0).getName())) {
            list.get(0).setName("全部");
        }

        binding.tabSecond.removeAllTabs();
        for (int i = 0; i < typeDatas.size(); i++) {
            binding.tabSecond.addTab(binding.tabSecond.newTab().setText(typeDatas.get(i).getName()));
        }
        binding.tabSecond.getTabAt(0).select();
        popWindow.setModelData(typeDatas);
    }

    public void showFilterSpec(List<DictEntity3> data) {
//        viewModel.filterJson.get().setAttributeList(new ArrayList<>());//清空之前的属性参数
//        if (data == null) {
//            popWindow.setSpecData(new ArrayList<>());
//            return;
//        }
//        popWindow.setSpecData(data);
    }

    /**
     * 排序数据
     */
    public void setSortData(List<DictEntity3> datas) {
        this.sortData = datas;
        if (sortData != null && sortData.size() > 0) {
            sortPopWindow.setData(sortData);
            popWindow.setSortData(datas);
        }
    }

    /**
     * 省市区数据信息
     *
     * @param data
     */
    public void showAddressDatas(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data, String provinceNo) {
        this.addressEntityList = data;
        if (data != null && data.size() > 0) {
            this.provinceNo = provinceNo;
            mAddressPop.setData(data, true, provinceNo);
        }
    }

    private void checkDismiss(int resId) {
        if (mAddressPop != null && mAddressPop.isShowing() && R.id.area_layout != resId) {
            mAddressPop.dismiss();
        }
    }

    public int getCheckIndex(List<DictEntity3> list, String checkedId) {
        if (list == null || list.size() == 0 || checkedId == null) {
            return 0;
        }
        for (int i = 0; i < list.size(); i++) {
            if (checkedId.equals(list.get(i).getId())) {
                return i;
            }

        }
        return 0;
    }

    private List<String> getCityList(List<BaseAreaEntity> list) {
        cityList = new ArrayList<>();
        for (BaseAreaEntity entity : list) {
            if (!TextUtils.isEmpty(entity.getNo())) {
                cityList.add(entity.getNo());
            }
        }
        return cityList;
    }

    //更新选项颜色
    private void updateChooseTextStatus(View view, TextView textView, int value, String name) {
        if (value == 0) {
            setTextViewBg(view, textView, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
        } else if (value == -1) {
            setTextViewBg(view, textView, R.drawable.search_bg, R.mipmap.ic_up_arrow_n, R.color.color_333333);
        } else if (value == 1) {
            setTextViewBg(view, textView, R.drawable.search_new_blue_bg, R.mipmap.icon_up_red, R.color.color_main);
        } else if (value == 2) {
            setTextViewBg(view, textView, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
        }
        if (TextUtils.isEmpty(name)) return;
        textView.setText(name);
    }

    //设置背景色和图标
    private void setTextViewBg(View view, TextView textView, int bg, int img, int textColor) {
        view.setBackgroundResource(bg);
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable_n, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

    public void startMap() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), SelectAddressActivity.class);
        intent.putExtra(SelectAddressActivity.TITLE, "详细地址");
        intent.putExtra(SelectAddressActivity.HINT, "请输入地址");
        intent.putExtra(SelectAddressActivity.SAVE_TEXT, "确定");
        if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData("distance_latitude", ""))) {
            intent.putExtra(SelectAddressActivity.LATITUDE, Double.parseDouble(SharedPreferenceUtil.getInstance().getSaveStringData("distance_latitude", "")));
            intent.putExtra(SelectAddressActivity.LONGTITUDE, Double.parseDouble(SharedPreferenceUtil.getInstance().getSaveStringData("distance_longitude", "")));
        }
        startActivityForResult(intent, 255);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 255 && data != null) {
                double lat = data.getDoubleExtra(SelectAddressActivity.LATITUDE, 0);
                double lon = data.getDoubleExtra(SelectAddressActivity.LONGTITUDE, 0);
                String address = data.getStringExtra(SelectAddressActivity.CURRENT_POSITION);
                //保存定位数据,用来给后端传参计算距离
                SharedPreferenceUtil.getInstance().saveData("distance_latitude", String.valueOf(lat));
                SharedPreferenceUtil.getInstance().saveData("distance_longitude", String.valueOf(lon));
                EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_LEASE_FILTER));
                loadData(false);
            }
        }
    }
}
